package br.usp.pcs.basys.engine;




public class Memory implements ComputerResource {

	private long memorySize;
	private long avaiableMemorySize;

	public long getMemorySize() {
		return memorySize;
	}
	
	public void setMemorySize(long memorySize) {
		this.avaiableMemorySize += memorySize-this.memorySize;
		this.memorySize = memorySize;
	}

	public void allocate(long size) throws UnavaiableResourceException{
		if (size<0){
			throw new IllegalArgumentException();
		}
		if (avaiableMemorySize-size>=0){
			this.avaiableMemorySize -= size;
		}
		else {
			throw new UnavaiableResourceException();
		}
	}
	
	public void deallocate(long size){
		if (size<0){
			throw new IllegalArgumentException();
		}
		assert (this.avaiableMemorySize + size <= memorySize);
		this.avaiableMemorySize += size;
	}

}
